﻿#include "pch.h"

//extern WoServerClass* g_app;
//@隐藏{
static WoServerClass* g_app=NULL;
//@隐藏}

extern WoLoggetPtr g_logger;

void WEBOAT_API WoInit() {
	WoRefInit();
#ifdef _WIN32
#ifdef _DEBUG
	//开启内存泄漏捕获
	//_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
#endif // _DEBUG
#endif // _WIN32
	if (g_app==NULL) {
		oatpp::web::protocol::http::Header::Value::SERVER = PROJECTNAME "/" PROJECTVVERSION;
		oatpp::web::protocol::http::Header::Value::USER_AGENT = PROJECTNAME "/" PROJECTVVERSION;
		oatpp::base::Environment::init();
		auto wr = WoCreateRouter();
		g_app = new WoServerClass(wr, WoCreateHttpConnectionHander(wr));
		//WoHTTPSetGlobalHandlerError(GlobalHandlerError);
		g_logger = WoLoggerCreateConsoleLogger("WEBOAT");
		g_logger->info("欢迎使用WEBOAT");
	}

	//SetHttpRequestHandlerErrorCallback(GlobalHandlerError);
};

void WEBOAT_API WoDestroy() {
	if (g_app!=NULL) {
		/*if (g_app->IsRuning()) {
			g_app->Stop();
		}*/
		delete g_app;
		g_app = NULL;
	}
	
	oatpp::base::Environment::destroy();
};

WoServerClass& WEBOAT_API WoGetAppServer() {
	try {
		if (g_app==nullptr) {
			throw "请先初始化环境";
		}
		return *g_app;
	}
	catch (const std::exception&err) {
		std::cout << err.what();
	}
	return *g_app;
};